//
//  LookAtLogisticsViewController.m
//  lmq
//
//  Created by 马田成 on 2017/4/12.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "LookAtLogisticsViewController.h"
#import "LookAtLogisticsTableViewCell.h"
#import "md5WWLIU.h"
#import "MF_Base64Additions.h"


@interface LookAtLogisticsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation LookAtLogisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationDate];
    [self creationView];
    [self regeditCell];
    
    
    
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    self.navigationItem.title = @"物流信息";
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64);
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}
- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LookAtLogisticsTableViewCell" bundle:nil] forCellReuseIdentifier:@"LookAtLogisticsTableViewCell"];
    
}

#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 110;
    
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LookAtLogisticsTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"LookAtLogisticsTableViewCell" forIndexPath:indexPath];
    NSMutableDictionary *dic = self.dataArr[indexPath.row];
    cell.timeLabel.text = [dic objectForKey:@"AcceptTime"];
    cell.contentLabel.text = [dic objectForKey:@"AcceptStation"];
    
    
    
    return cell;
    
    
}
- (void)creationDate{
    
    _dataArr = [[NSMutableArray alloc]init];
//    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
//    [productParams setValue:_courier forKey:@"LogisticCode"];
//    [productParams setValue:_kd_name forKey:@"ShipperCode"];
//    [productParams setValue:_order_id forKey:@"OrderCode"];
    NSString *EBusinessID = @"1279080";
    NSString *AppKey = @"fae865c0-deb2-4fc3-b6d7-512d9bf87d49";
//    NSString *RequestType = @"1008";
    
    
    
    NSString* shipperCode = _kd_name;
    //获取快递单号
    NSString* logisticCode = _courier;
    //根据快递鸟api请求的要求格式进行数据的处理
    //其中eBusinessID,appKey是快递鸟api提供
    //reqURL：请求的网址，同样是由快递鸟api提供
    NSString* requestData = [NSString stringWithFormat:@"{\"OrderCode\":\"\",\"ShipperCode\":\"%@\",\"LogisticCode\":\"%@\"}",shipperCode,logisticCode];
    NSMutableDictionary* params = [[NSMutableDictionary alloc]init];
    NSString* dataSignTmp = [[NSString alloc]initWithFormat:@"%@%@",requestData,AppKey];
    //这里使用到了MD5加密程序，以及Base64编码
    NSString* dataSign = [[md5WWLIU md5String:dataSignTmp] base64String];
    [params setObject:[requestData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"RequestData"];
    [params setObject:EBusinessID forKey:@"EBusinessID"];
    [params setObject:@"1002" forKey:@"RequestType"];
    [params setObject:[dataSign stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"DataSign"];
    [params setObject:@"2" forKey:@"DataType"];
    
    
    
    
    [[MNetworkHandler sharedInstance]conURL:@"http://api.kdniao.cc/api/dist"
                                networkType:NetWorkPOST
                                     params:params
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([returnData objectForKey:@"Traces"]) {
                                       
                                       _dataArr=[returnData objectForKey:@"Traces"];
                                       ;
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"Reason"] duration:5.0f];

                                       
                                       [self.tableView reloadData];
                                   }
                                   else{
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"Reason"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
